package admin

import org.springframework.dao.DataIntegrityViolationException

class AtuacaoController {

      static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

      def index() {
            redirect(action: "list", params: params)
      }

      def list() {
            params.max = Math.min(params.max ? params.int('max') : 10, 100)
            [atuacaoInstanceList: Atuacao.list(params), atuacaoInstanceTotal: Atuacao.count()]
      }

      def create() {
            [atuacaoInstance: new Atuacao(params)]
      }

      def save() {
            def atuacaoInstance = new Atuacao(params)
            if (!atuacaoInstance.save(flush: true)) {
                  render(view: "create", model: [atuacaoInstance: atuacaoInstance])
                  return
            }

            flash.message = message(code: 'default.created.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), atuacaoInstance.id])
            redirect(action: "show", id: atuacaoInstance.id)
      }

      def show() {
            def atuacaoInstance = Atuacao.get(params.id)
            if (!atuacaoInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), params.id])
                  redirect(action: "list")
                  return
            }

            [atuacaoInstance: atuacaoInstance]
      }

      def edit() {
            def atuacaoInstance = Atuacao.get(params.id)
            if (!atuacaoInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), params.id])
                  redirect(action: "list")
                  return
            }

            [atuacaoInstance: atuacaoInstance]
      }

      def update() {
            def atuacaoInstance = Atuacao.get(params.id)
            if (!atuacaoInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), params.id])
                  redirect(action: "list")
                  return
            }

            if (params.version) {
                  def version = params.version.toLong()
                  if (atuacaoInstance.version > version) {
                        atuacaoInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                              [message(code: 'atuacao.label', default: 'Atuacao')] as Object[],
                          "Another user has updated this Atuacao while you were editing")
                        render(view: "edit", model: [atuacaoInstance: atuacaoInstance])
                        return
                  }
            }

            if(params.imagem.size > 0){
                  atuacaoInstance.properties = params
                  def im = "imagem"          
                  def f = request.getFile(im)
                  atuacaoInstance.imagem = f.getBytes()
            }
            else{
                  atuacaoInstance.titulo = params.titulo
                  atuacaoInstance.descricao = params.descricao
            }

            if (!atuacaoInstance.save(flush: true)) {
                  render(view: "edit", model: [atuacaoInstance: atuacaoInstance])
                  return
            }

            flash.message = message(code: 'default.updated.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), atuacaoInstance.id])
            redirect(action: "show", id: atuacaoInstance.id)
      }

      def delete() {
            def atuacaoInstance = Atuacao.get(params.id)
            if (!atuacaoInstance) {
                  flash.message = message(code: 'default.not.found.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), params.id])
                  redirect(action: "list")
                  return
            }

            try {
                  atuacaoInstance.delete(flush: true)
                  flash.message = message(code: 'default.deleted.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), params.id])
                  redirect(action: "list")
            }
            catch (DataIntegrityViolationException e) {
                  flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'atuacao.label', default: 'Atuacao'), params.id])
                  redirect(action: "show", id: params.id)
            }
      }
    
      def view = {
            def imagem = Atuacao.get( params.id )
            if (imagem) {
                  response.setContentLength(imagem.imagem.size())
                  response.setHeader('filename', imagem.descricao)
                  OutputStream out = response.outputStream
                  out.write(imagem.imagem)
                  out.close()
            }
            else {
                  response.sendError(404)
            }
      }
}
